//import 'package:example/special_text/image_text.dart';
import 'package:extended_text_library/extended_text_library.dart';
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';

import 'whl_at_text.dart';
import 'whl_emoji_text.dart';

class WhlSpecialTextSpanBuilder extends SpecialTextSpanBuilder {
  WhlSpecialTextSpanBuilder({this.showAtBackground = false});

  /// whether show background for @somebody
  final bool showAtBackground;
  @override
  TextSpan build(String data,
      {TextStyle? textStyle, SpecialTextGestureTapCallback? onTap}) {
    if (kIsWeb) {
      return TextSpan(text: data, style: textStyle);
    }

    return super.build(data, textStyle: textStyle, onTap: onTap);
  }

  @override
  SpecialText? createSpecialText(String flag,
      {TextStyle? textStyle, SpecialTextGestureTapCallback? onTap, required int index}) {
    if (flag == null || flag == '') {
      return null;
    }

    ///index is end index of start flag, so text start index should be index-(flag.length-1)
    if (isStart(flag, WhlEmojiText.flag)) {
      return WhlEmojiText(textStyle, start: index - (WhlEmojiText.flag.length - 1));
    }
//    else if (isStart(flag, ImageText.flag)) {
//      return ImageText(textStyle,
//          start: index - (ImageText.flag.length - 1), onTap: onTap);
//    }
    else if (isStart(flag, WhlAtText.flag)) {
      return WhlAtText(
        textStyle,
        onTap,
        start: index - (WhlAtText.flag.length - 1),
        showAtBackground: showAtBackground,
      );
    } else if (isStart(flag, WhlEmojiText.flag)) {
      return WhlEmojiText(textStyle, start: index - (WhlEmojiText.flag.length - 1));
    }
    return null;
  }
}
